﻿namespace Pub.Class
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Xml;
    using System.Xml.Linq;
    using System.Xml.Serialization;

    public static class XmlNodeExtensions
    {
        public static XmlCDataSection CreateCDataSection(this XmlNode parentNode)
        {
            return parentNode.CreateCDataSection(string.Empty);
        }

        public static XmlCDataSection CreateCDataSection(this XmlNode parentNode, string data)
        {
            XmlCDataSection newChild = ((parentNode is XmlDocument) ? ((XmlDocument) parentNode) : parentNode.OwnerDocument).CreateCDataSection(data);
            parentNode.AppendChild(newChild);
            return newChild;
        }

        public static XmlNode CreateChildNode(this XmlNode parentNode, string name)
        {
            XmlNode newChild = ((parentNode is XmlDocument) ? ((XmlDocument) parentNode) : parentNode.OwnerDocument).CreateElement(name);
            parentNode.AppendChild(newChild);
            return newChild;
        }

        public static XmlNode CreateChildNode(this XmlNode parentNode, string name, string namespaceUri)
        {
            XmlNode newChild = ((parentNode is XmlDocument) ? ((XmlDocument) parentNode) : parentNode.OwnerDocument).CreateElement(name, namespaceUri);
            parentNode.AppendChild(newChild);
            return newChild;
        }

        public static T FromXDoc<T>(this XDocument xdoc)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (XmlReader reader = xdoc.CreateReader())
            {
                return (T) serializer.Deserialize(reader);
            }
        }

        public static string GetAttribute(this XmlNode node, string attributeName)
        {
            return node.GetAttribute(attributeName, null);
        }

        public static T GetAttribute<T>(this XmlNode node, string attributeName)
        {
            return node.GetAttribute<T>(attributeName, default(T));
        }

        public static string GetAttribute(this XmlNode node, string attributeName, string defaultValue)
        {
            XmlAttribute attribute = node.Attributes[attributeName];
            if (!attribute.IsNotNull())
            {
                return defaultValue;
            }
            return attribute.InnerText;
        }

        public static T GetAttribute<T>(this XmlNode node, string attributeName, T defaultValue)
        {
            string attribute = node.GetAttribute(attributeName);
            if (attribute.IsNullEmpty())
            {
                return defaultValue;
            }
            return attribute.ConvertTo<T>(defaultValue);
        }

        public static string GetCDataSection(this XmlNode parentNode)
        {
            foreach (object obj2 in parentNode.ChildNodes)
            {
                if (obj2 is XmlCDataSection)
                {
                    return ((XmlCDataSection) obj2).Value;
                }
            }
            return null;
        }

        public static void SetAttribute(this XmlNode node, string name, object value)
        {
            node.SetAttribute(name, value.IsNotNull() ? value.ToString() : null);
        }

        public static void SetAttribute(this XmlNode node, string name, string value)
        {
            if (node.IsNotNull())
            {
                XmlAttribute attribute = node.Attributes[name, node.NamespaceURI];
                if (attribute.IsNull())
                {
                    attribute = node.OwnerDocument.CreateAttribute(name, node.OwnerDocument.NamespaceURI);
                    node.Attributes.Append(attribute);
                }
                attribute.InnerText = value;
            }
        }
    }
}

